package com.wwc.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwc.common.context.LoginMemberContext;
import com.wwc.common.exception.BusinessException;
import com.wwc.common.exception.BusinessExceptionEnum;
import com.wwc.common.response.PageResponse;
import com.wwc.common.utils.SnowUtil;
import com.wwc.member.controller.dto.passenger.PassengerDeleteDTO;
import com.wwc.member.controller.dto.passenger.PassengerInsertAndUpdateDTO;
import com.wwc.member.controller.dto.passenger.PassengerSelectListDTO;
import com.wwc.member.controller.vo.PassengerSelectListVO;
import com.wwc.member.entity.Passenger;
import com.wwc.member.mapper.PassengerMapper;
import com.wwc.member.service.IPassengerService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class PassengerServiceImpl extends ServiceImpl<PassengerMapper, Passenger> implements IPassengerService{
	@Resource
	private PassengerMapper passengerMapper;

	@Override
	public String insertAndUpdate(PassengerInsertAndUpdateDTO passengerInsertAndUpdateDTO){
		Passenger passenger = BeanUtil.copyProperties(passengerInsertAndUpdateDTO, Passenger.class);
		// 判断id是否为空 id为空则新增，不为空则更新
		if(ObjectUtil.isNull(passenger.getId())){
			passenger.setId(SnowUtil.getSnowflakeNextId());
			passenger.setMemberId(LoginMemberContext.getId());
			passenger.setCreateTime(new Date());
			passenger.setUpdateTime(new Date());
			passengerMapper.insert(passenger);
			return "新增乘车人成功！";
		}else{
			passenger.setUpdateTime(new Date());
			passengerMapper.updateById(passenger);
			return "更新乘车人成功！";
		}
	}

	@Override
	public PageResponse<PassengerSelectListVO> selectListPage(PassengerSelectListDTO passengerSelectListDTO){
		// 根据分页配置和查询条件获取乘车人分页列表
		Page<Passenger> page = new Page<>(passengerSelectListDTO.getPage(), passengerSelectListDTO.getSize());
		QueryWrapper<Passenger> passengerQueryWrapper = new QueryWrapper<>();
		passengerQueryWrapper.eq("member_id", passengerSelectListDTO.getMemberId());
		passengerQueryWrapper.orderByDesc("update_time");
		Page<Passenger> passengerPage = passengerMapper.selectPage(page, passengerQueryWrapper);

		// 创建自定义分页配置类
		PageResponse<PassengerSelectListVO> pageResponse = new PageResponse<>();
		pageResponse.setTotal(passengerPage.getTotal());
		pageResponse.setList(BeanUtil.copyToList(passengerPage.getRecords(), PassengerSelectListVO.class));
		return pageResponse;
	}

	@Override
	public void delete(PassengerDeleteDTO passengerDeleteDTO){
		// 判断想要删除的数据是否存在
		Passenger p = passengerMapper.selectById(passengerDeleteDTO.getId());
		if(ObjectUtil.isNull(p)){
			throw new BusinessException(BusinessExceptionEnum.MODIFIED_DELETE_DATA_DOES_NOT_EXIST);
		}

		passengerMapper.deleteById(passengerDeleteDTO.getId());
	}
}
